package chapter13;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2019/2/20 18:16
 */
public class ReaderWriterTest {
    public static void main(String[] args) {
        String filePath = "D:\\hello.txt";
//        testWriter(filePath);
//        testReader(filePath);
//        testCharArrayWriter(filePath);
//        testStringWriter(filePath);

        List<Student> students = Arrays.asList(new Student[]{
                new Student("张三", 18, 89.87),
                new Student("李四", 17, 90.1)
        });
/*
        testBufferedWriter(students, filePath);*/

//        testBufferedReader(filePath);

//        testPrintWriter(students, filePath);

        String str = "'张三',18,90.1";
        Scanner scanner = new Scanner(str).useDelimiter(",");
        System.out.println(scanner.next());
        System.out.println(scanner.nextInt());
        System.out.println(scanner.nextDouble());
    }

    private static void testPrintWriter(List<Student> students, String filePath) {
        try (PrintWriter writer = new PrintWriter(filePath)) {
            for (Student student : students) {
                writer.println(student);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    private static void testBufferedReader(String filePath) {
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            List<Student> students = new ArrayList<>();
            String line = reader.readLine();
            while (line != null) {
                int startIndex = line.indexOf("{");
                int endIndex = line.indexOf("}");
                String data = line.substring(startIndex + 1, endIndex);
                String[] attris = data.split(",");
                Student student = new Student(getSecond(attris[0], "="), Integer.parseInt(getSecond(attris[1], "=")), Double.parseDouble(getSecond(attris[2], "=")));
                students.add(student);
                line = reader.readLine();
            }

            System.out.println(students);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static String getSecond(String str, String splitter) {
        String[] strings = str.split(splitter);
        if (strings.length < 2) {
            throw new IllegalArgumentException("字符分割数小于2");
        }
        return strings[1];
    }

    private static void testBufferedWriter(List<Student> students, String filePath) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath, true))) {
            for (Student student : students) {
                writer.write(student.toString());
                writer.newLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void testStringWriter(String filePath) {
        try (Reader reader = new FileReader(filePath)) {
            char[] cbuf = new char[1024];
            int byteRead = 0;
            StringWriter writer = new StringWriter();
            while ((byteRead = reader.read(cbuf)) != -1) {
                writer.write(cbuf, 0, byteRead);
            }
            System.out.println(writer.toString());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void testCharArrayWriter(String filePath) {
        try (Reader reader = new FileReader(filePath)) {
            CharArrayWriter writer = new CharArrayWriter();
            char[] cbuf = new char[1024];
            int charRead = 0;
            while ((charRead = reader.read(cbuf)) != -1) {
                writer.write(cbuf, 0, charRead);
            }
            System.out.println(writer.toString());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void testWriter(String filePath) {
        try (Writer writer = new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(filePath, true)), "gb2312")) {
            String str = "hello world";
            writer.write(str);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void testReader(String filePath) {
        try (Reader reader = new InputStreamReader(new FileInputStream(filePath), "gb2312")) {
            char[] chuf = new char[1024];
            int charsRead = reader.read(chuf);
            System.out.println(new String(chuf, 0, charsRead));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
